package com.eqdd.yiqidian.ui.mine.qiye;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.eqdd.annotation.datasave.DealCodeClass;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.SectionDividerItemDecoration;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.annotation.DealCode;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.CodeRule;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.EnterBean;
import com.eqdd.yiqidian.bean.SecondArrowBean;
import com.eqdd.yiqidian.bean.SecondBean;
import com.eqdd.yiqidian.bean.SecondIconBean;
import com.eqdd.yiqidian.bean.rebuild.CompanyInfoResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.rxbus2.Subscribe;
import com.eqdd.yiqidian.rxbus2.ThreadMode;
import com.eqdd.yiqidian.ui.base.BaseEnterActivity;
import com.eqdd.yiqidian.ui.base.EditTextActivity;
import com.eqdd.yiqidian.ui.mine.xiaoxi.ContactBookNewsActivity;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.PicUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.luck.picture.lib.PictureSelector;

import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lvzhihao on 17-4-15.
 */

public class QiyeActivity extends BaseEnterActivity {
    private Class[] classes = new Class[]{
            QiyeRegisterActivity.class,
            QiyeAuActivity.class,
            ComInvitedActivity.class,
            ShowMyContractActivity.class,
            ContactBookNewsActivity.class
    };
    ArrayList<Object> secondBeen = new ArrayList<>();
    private SlimAdapterEx slimAdapterEx;
    private int isauthen = -1;

    @Subscribe(codes = CodeRule.USER_INFO, threadMode = ThreadMode.MAIN)
    public void refreshUser() {
        refresh();
    }

    @DealCodeClass(classes = {ComInvitedActivity.class})
    public void updateRuzhi(int num) {
        updateNum(2, num);
    }

    @DealCodeClass(classes = {ShowMyContractActivity.class})
    public void updateLaodonghetong(int num) {
        updateNum(3, num);
    }

    @DealCodeClass(classes = {ContactBookNewsActivity.class})
    public void updateLianluoshu(int num) {
        updateNum(4, num);
    }

    @Override
    public void itemClick(int p) {

        if (classes[p] != null) {
            if (p == 0) {
                if (AppConstent.user.getCompanyId() == 0) {
                    startActivity(classes[p]);
                } else {
                    ToastUtil.showShort("你已加入企业");
                }
            } else if (p == 1) {
                if (isauthen == 0) {
                    startActivity(classes[p]);
                } else if (isauthen > 0) {
                    ToastUtil.showShort("你所在的企业已完成认证");
                } else if (isauthen == -1) {
                    ToastUtil.showShort("你还没有企业");

                }
            } else {
                startActivity(classes[p]);
            }
        } else {
            ToastUtil.showShort("该模块暂未开发");
        }
    }

    @Override
    public void initEnterData() {
        isDealCode = true;
        initTopTitleBar(View.VISIBLE, "我的企业");
        enterBeans.add(new EnterBean("企业注册"));
        enterBeans.add(new EnterBean("企业认证"));
        enterBeans.add(new EnterBean("入职邀请").setCode(CodeRule.STAFF_RUZHI));
        enterBeans.add(new EnterBean("签订合同").setCode(CodeRule.STAFF_LAODONGHETONG));
        enterBeans.add(new EnterBean("联络书").setCode(CodeRule.BEILIANLU_LIANLUOSHU));
        dataBinding.recyclerView.addItemDecoration(new SectionDividerItemDecoration(this, enterBeans.size()).setLastBootomHeght(0).setHeadHeight(0));


    }

    @Override
    public void setDownView() {
        dataBinding.tvNextTitle.setVisibility(View.VISIBLE);
        dataBinding.tvNextTitle.setText("公司信息");
        dataBinding.recyclerViewDown.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerViewDown.setNestedScrollingEnabled(false);
        if (slimAdapterEx == null) {
            dataBinding.recyclerViewDown.setLayoutManager(new LinearLayoutManager(this));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_08, new SlimInjector<SecondBean>() {
                @Override
                public void onInject(SecondBean data, IViewInjector injector) {
                    injector.text(R.id.tv_upper_content, data.getFirst())
                            .with(R.id.tv_down_content, v -> {
                                TextView textView = (TextView) v;
                                textView.setText(data.getSecond());
                                textView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
                            })
                            .invisible(R.id.iv_right_enter);
                }
            }).register(R.layout.list_item_23, new SlimInjector<SecondIconBean>() {
                @Override
                public void onInject(SecondIconBean data, IViewInjector injector) {
                    injector.text(R.id.tv_left_content, data.getFirst())
                            .with(R.id.iv_right_logo, v ->
                                    ImageUtil.setCircleImage(data.getSecond(), (ImageView) v)
                            );
                }
            }).register(R.layout.list_item_08, new SlimInjector<SecondArrowBean>() {
                @Override
                public void onInject(SecondArrowBean data, IViewInjector injector) {
                    injector.text(R.id.tv_upper_content, data.getFirst())
                            .with(R.id.tv_down_content, v -> {
                                TextView textView = (TextView) v;
                                textView.setText(data.getSecond());
                                textView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
                            });
                }
            })
                    .attachTo(dataBinding.recyclerViewDown);
            ItemClickSupport.addTo(dataBinding.recyclerViewDown)
                    .setOnItemClickListener((rv, position, v) -> {

                        if (slimAdapterEx.getData().get(position) instanceof SecondIconBean) {
                            PicUtil.single(QiyeActivity.this, true);

                        } else if (slimAdapterEx.getData().get(position) instanceof SecondArrowBean) {
                            SecondArrowBean secondArrowBean = (SecondArrowBean) slimAdapterEx.getData().get(position);
                            Bundle bundle = new Bundle();
                            bundle.putString("title", "修改企业简称");
                            bundle.putString("tip", "修改企业简称");
                            bundle.putString("content", secondArrowBean.getSecond());
                            startActivityForResult(EditTextActivity.class, bundle, Config.EDIT_TEXT);
                        }
                    });
        }
        refresh();
    }

    private void refresh() {
        OkGo.<HttpResult<CompanyInfoResultBean>>post(HttpConfig.BASE_URL + HttpConfig.GET_COM_SIMPLE_INFO)
                .params("comId", AppConstent.user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<CompanyInfoResultBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<CompanyInfoResultBean>> response) {
                        HttpResult<CompanyInfoResultBean> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            isauthen = httpResult.getItems().getIsauthen();
                            ArrayList<Object> temp = new ArrayList<>();
                            secondBeen.clear();
                            secondBeen.add(new SecondBean("企业全称", httpResult.getItems().getName()));
                            secondBeen.add(new SecondArrowBean("企业简称", httpResult.getItems().getSimpleName()));
                            secondBeen.add(new SecondIconBean("企业LOGO", httpResult.getItems().getLogo()));
                            secondBeen.add(new SecondBean("企业类型", httpResult.getItems().getType()));
                            secondBeen.add(new SecondBean("企业行业", httpResult.getItems().getHangye()));
                            secondBeen.add(new SecondBean("企业地址", httpResult.getItems().getAddress()));
                            secondBeen.add(new SecondBean("企业邮箱", httpResult.getItems().getEmail()));
                            temp.addAll(secondBeen);
                            slimAdapterEx.updateData(temp);
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.EDIT_TEXT && resultCode == Config.INTENT_CONTENT) {
            String content = data.getStringExtra("content");
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_QIYE_JIANCHENG)
                    .params("userGuid", user.getGuid())
                    .params("comId", user.getCompanyId())
                    .params("simpleName", content)
                    .execute(new DialogCallBack<HttpResult>(this) {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                            HttpResult httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                            if (httpResult.getStatus() == 200) {
                                ((SecondArrowBean) slimAdapterEx.getData().get(1)).setSecond(content);
                                slimAdapterEx.notifyItemChanged(1);
                            }
                        }
                    });
        }
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                LocalMedia localMedia = selectList.get(0);
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_QIYE_LOGO)
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("comId", AppConstent.user.getCompanyId())
                        .params("logo", new File(localMedia.isCompressed() ? localMedia.getCompressPath() : localMedia.getPath()))
                        .execute(new DialogCallBack<HttpResult>(QiyeActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    refresh();
                                }
                            }
                        });
            }
        }
    }
}
